﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem027
    {
        /// <summary>
        /// Find the product of the coefficients, a and b, for the quadratic expression that 
        /// produces the maximum number of primes for consecutive values of n, starting with n = 0.
        /// </summary>
        public static long Solve()
        {
            Func<long, long, long, long> quadratic = (n, a, b) => (n * n) + (a * n) + b;

            //Enumerable.Range(-999, 999 * 2).SelectMany(a =>
            //Enumerable.Range(-999, 999 * 2).TakeWhile(b =>
            //    quadratic(0, a, b).IsPrime())

            long product = 0;
            long maxCount = 0;
            for (int a = -999; a < 999; a++)
            {
                for (int b = -999; b < 999; b++)
                {
                    int count = 0;
                    for (int n = 0; ; n++)
                    {
                        long result = quadratic(n, a, b);
                        if (!((int)result).IsPrime())
                            break;

                        count++;
                    }
                    if (count > maxCount)
                    {
                        product = a * b;
                        maxCount = count;
                    }
                }
            }

            return product;
        }
    }
}
